package com.ht.dsp.impl;

import _61970.meas.impl.AnalogValueImpl;
import _61970.meas.impl.DiscreteValueImpl;
import com.ht.aems.ntp.IResourceManager;
import com.ht.aems.ntp.SystemModel;
import com.ht.aems.util.FTPOperator;
import org.apache.log4j.Logger;

import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

/**
 * Created by IntelliJ IDEA.
 *
 * @author Dong Shufeng
 *         Date: 2010-1-22
 *         Time: 10:24:30
 */
public class MeasurePointWriter_ge {
    private static Logger log = Logger.getLogger(MeasurePointWriter_ge.class);
    private Map<Integer, String> points_anglog = new HashMap<Integer, String>(3000);
    private Map<Integer, String> points_discrete = new HashMap<Integer, String>(5000);

    public void writerFile(String file) throws IOException {
        StringBuffer buffer = new StringBuffer();
        for (Integer p : points_anglog.keySet())
            buffer.append("2_").append(p).append("\n");
        for (Integer p : points_discrete.keySet())
            buffer.append("1_").append(p).append("\n");
        FileWriter fw = new FileWriter(file);
        ByteArrayInputStream s = new ByteArrayInputStream(buffer.toString().getBytes("GB2312"));
        int c;
        while ((c = s.read()) != -1)
            fw.write(c);
        log.info(points_anglog.size() + " analog points and " + points_discrete.size() + " discrete points has been written to file : " + file);
        fw.close();
    }

    public void uploadToFtp(String url, int port, String user, String password, String path, String fileName) throws IOException {
        StringBuffer buffer = new StringBuffer();
        for (Integer p : points_anglog.keySet())
            buffer.append("2_").append(p).append("\n");
        for (Integer p : points_discrete.keySet())
            buffer.append("1_").append(p).append("\n");
        ByteArrayInputStream stream = new ByteArrayInputStream(buffer.toString().getBytes());
        FTPOperator ftp = new FTPOperator();
        ftp.connectServer(url, port, user, password, path);
        ftp.uploadFile(stream, fileName);
        log.info(points_anglog.size() + " analog points and " + points_discrete.size() + " discrete points has been uploaded to ftp server: " + url);
        ftp.closeServer();
    }

    public void updateAnalogFromCIM() {
        IResourceManager resource = SystemModel.INSTANCE.getResourceManager();
        List allAnalogValue = resource.getResource(AnalogValueImpl.class);
        for (Object amea : allAnalogValue) {
            AnalogValueImpl am = (AnalogValueImpl) amea;
            int p = Integer.parseInt(am.getName());
            if (!points_anglog.containsKey(p))
                points_anglog.put(p, am.getMRID());
        }
    }

    public void updateDiscreteFromCIM() {
        IResourceManager resource = SystemModel.INSTANCE.getResourceManager();
        List allDiscreteValue = resource.getResource(DiscreteValueImpl.class);
        for (Object amea : allDiscreteValue) {
            DiscreteValueImpl am = (DiscreteValueImpl) amea;
            int p = Integer.parseInt(am.getName());
            if (!points_discrete.containsKey(p))
                points_discrete.put(p, am.getMRID());
        }
    }

    public void updateAGCPoint(String filePath) throws IOException {
        FileInputStream fis;
        fis = new FileInputStream(filePath);
        BufferedReader fin = new BufferedReader(new InputStreamReader(fis, "GB2312"));
        String s = fin.readLine();
        int count = 0;
        while (s != null) {
            s = s.trim();
            String[] ss = s.split("\t");
            if (ss.length == 1) {
                int p = Integer.parseInt(ss[0]);
                if (!points_discrete.containsKey(p)) {
                    points_discrete.put(p, "agc_" + count);
                    count++;
                }
            } else if (ss.length == 5) {
                int p1 = Integer.parseInt(ss[0]);
                if (!points_discrete.containsKey(p1)) {
                    points_discrete.put(p1, "agc_" + count);
                    count++;
                }
                int p2 = Integer.parseInt(ss[0]);
                if (!points_discrete.containsKey(p2)) {
                    points_discrete.put(p2, "agc_" + count);
                    count++;
                }
                int p3 = Integer.parseInt(ss[0]);
                if (!points_discrete.containsKey(p3)) {
                    points_discrete.put(p3, "agc_" + count);
                    count++;
                }
                int p4 = Integer.parseInt(ss[0]);
                if (!points_discrete.containsKey(p4)) {
                    points_discrete.put(p4, "agc_" + count);
                    count++;
                }
            }
            s = fin.readLine();
        }
        System.out.println("Agc discrete points number:" + count);
        fis.close();
        fin.close();
    }

    public void updateStationPoint(String filePath) throws IOException {
        FileInputStream fis;
        fis = new FileInputStream(filePath);
        BufferedReader fin = new BufferedReader(new InputStreamReader(fis, "GB2312"));
        String s = fin.readLine();
        int count = 0;
        while (s != null) {
            s = s.trim();
            String[] ss = s.split("\t");
            int p1 = Integer.parseInt(ss[2]);
            if (!points_anglog.containsKey(p1)) {
                points_anglog.put(p1, "station_" + count);
                count++;
            }
            int p2 = Integer.parseInt(ss[3]);
            if (!points_anglog.containsKey(p2)) {
                points_anglog.put(p2, "station_" + count);
                count++;
            }
            int p = Integer.parseInt(ss[4]);
            if (!points_discrete.containsKey(p)) {
                points_discrete.put(p, "station_" + count);
            }
            s = fin.readLine();
        }
        System.out.println("Station points number:" + count);
        fis.close();
        fin.close();
    }

    public void updateAdditionalPoints_analog(String propertyFile) throws IOException {
        Properties p = new Properties();
        p.load(new FileInputStream(propertyFile));
        for (Object obj : p.keySet()) {
            String key = obj.toString();
            String v = p.getProperty(key);
            int num = Integer.parseInt(v);
            if (!points_anglog.containsKey(num)) {
                points_anglog.put(num, key);
            }
        }
    }


    public void updateAnolgoToSavePoint(String filePath) throws IOException {
        FileInputStream fis;
        fis = new FileInputStream(filePath);
        BufferedReader fin = new BufferedReader(new InputStreamReader(fis, "GB2312"));
        String s = fin.readLine();
        int count = 0;
        while (s != null) {
            s = s.trim();
            if (!s.equals("")) {
                points_anglog.put(Integer.parseInt(s), "analogTosave_" + count);
            }
            count++;
            s = fin.readLine();
        }
        System.out.println("analogTosave points number:" + count);
        fis.close();
        fin.close();
    }

    public void updateDiscreteToSavePoint(String filePath) throws IOException {
        FileInputStream fis;
        fis = new FileInputStream(filePath);
        BufferedReader fin = new BufferedReader(new InputStreamReader(fis, "GB2312"));
        String s = fin.readLine();
        int count = 0;
        while (s != null) {
            s = s.trim();
            if (!s.equals("")) {
                points_discrete.put(Integer.parseInt(s), "discreteTosave_" + count);
            }
            count++;
            s = fin.readLine();
        }
        System.out.println("discreteTosave points number:" + count);
        fis.close();
        fin.close();
    }

    public void updateVarPoints_analog(String propertyFile) throws IOException {
        Properties p = new Properties();
        p.load(new FileInputStream(propertyFile));
        for (Object obj : p.keySet()) {
            String key = obj.toString().trim();
            String values = p.getProperty(key);
            String[] valu = values.split("[,]");
            if (valu.length != 3) {
                log.error("varpoint.properties " + key);
            } else {
                for (String value : valu) {
                    int num = Integer.parseInt(value.trim());
                    if (!points_anglog.containsKey(num)) {
                        points_anglog.put(num, key);
                    } else {
                        log.error("varpoint.properties " + num);
                    }
                }
            }
        }
    }

    public void updateAdditionalPoints_discrete(String propertyFile) throws IOException {
        Properties p = new Properties();
        p.load(new FileInputStream(propertyFile));
        for (Object obj : p.keySet()) {
            String key = obj.toString();
            String v = p.getProperty(key);
            int num = Integer.parseInt(v);
            if (!points_discrete.containsKey(num)) {
                points_discrete.put(num, key);
            }
        }
    }

    public static void main(String[] args) {

    }
}
